// pages/mine/room_register/camera/card/card.js
import Toast from '../../../../../miniprogram_npm/@vant/weapp/toast/toast'
import Dialog from '../../../../../miniprogram_npm/@vant/weapp/dialog/dialog'

const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        image: null,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 拍照
     * @param e
     */
    onCamera: function (e) {
        const _this = this
        const ctx = wx.createCameraContext()
        ctx.takePhoto({
            quality: 'high',
            success: res => {
                _this.setData({
                    image: res.tempImagePath
                })
                log.l("照片地址：" + res.tempImagePath)
            }
        })
    },

    /**
     * 确定
     * @param e
     */
    onConfirm: function (e) {
        const pages = getCurrentPages()
        const prevPage = pages[pages.length - 2]
        Toast.loading({
            duration: 0,
            mask: true,
            message: '正在上传'
        })
        http.upFile(this.data.image, 'IDPhoto').then(res => {
            if (res.code === http.REQUEST_OK) {
                Toast.clear()
                Toast.loading({
                    duration: 0,
                    mask: true,
                    message: '正在识别'
                })
                http.get('people_info/smart/identificationCard', {
                    // img_url: 'http://xinwangzn.com:85/files/wangEditer/20207/27/ff8080816ee07ef201738e1d3455004d.jpg'
                    img_url: res.data.url
                }).then(res2 => {
                    log.e("res2" + JSON.stringify(res2))
                    if (res2.data.code === http.REQUEST_OK) {
                        const result = res2.data.data.result_list[0]
                        if (result.code === 0) {
                            Toast.clear()
                            const regName = /^[\u4e00-\u9fa5]{2,4}$/;
                            if (regName.test(result.data.name)
                                && result.data.name.length >= 2
                                && app.isCardNo(result.data.id)) {
                                app.globalData.cardInfo = result.data
                                app.globalData.cardImageUrl = res.data.url
                                prevPage.setData({
                                    cardImageUrl: app.globalData.cardImageUrl
                                })
                                Toast.success({
                                    duration: 1000,
                                    message: '身份证识别成功'
                                })
                                setTimeout(() => {
                                    Toast.clear()
                                    wx.navigateBack()
                                }, 1000)
                            }else {
                                Toast.clear()
                                Dialog.alert({
                                    title: '温馨提示',
                                    message: '身份证识别失败，请重新拍摄'
                                }).then(() => {
                                    this.setData({
                                        image: null
                                    })
                                })
                            }
                        } else {
                            Toast.clear()
                            Dialog.alert({
                                title: '温馨提示',
                                message: '身份证识别失败，请重新拍摄'
                            }).then(() => {
                                this.setData({
                                    image: null
                                })
                            })
                        }
                    } else {
                        Toast.clear()
                        Dialog.alert({
                            title: '温馨提示',
                            message: '身份证识别失败，请重新拍摄'
                        }).then(() => {
                            this.setData({
                                image: null
                            })
                        })
                        log.e(JSON.stringify(res))
                    }
                }, err => {
                    Toast.clear()
                    Dialog.alert({
                        title: '温馨提示',
                        message: '身份证识别失败，请重新拍摄'
                    }).then(() => {
                        this.setData({
                            image: null
                        })
                    })
                    log.e(JSON.stringify(err))
                })
            } else {
                Toast.clear()
                Dialog.alert({
                    title: '温馨提示',
                    message: '上传失败，请重新拍摄'
                }).then(() => {
                    this.setData({
                        image: null
                    })
                })
            }
        }, err => {
            Toast.clear()
            Dialog.alert({
                title: '温馨提示',
                message: '上传失败，请重新拍摄'
            }).then(() => {
                this.setData({
                    image: null
                })
            })
        })
    },

    /**
     * 关闭 退出
     * @param e
     */
    onBack: function (e) {
        if (this.data.image == null) {
            wx.navigateBack()
        } else {
            this.setData({
                image: null
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})